package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.sam.im.heiqun.R;
/**
 * 加载网页的界面
 * Created by sun on 2017/3/1.
 */

public class WebViewActivity extends Activity{

    private WebView mWebView;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        initView();
        initData();
    }

    public static Intent newIntent(Context context, String url) {

        Intent intent=new Intent(context,WebViewActivity.class);
        intent.putExtra("url",url);
        return intent;
    }

    protected void initView() {
        mWebView = (WebView) findViewById(R.id.webview_webview);
       /*back = (LinearLayout) findViewById(R.id.webview_back_id);
        back.setOnClickListener(new View.OnClickListener() {
        } {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });*/
       back= (ImageView) findViewById(R.id.webview_back_id);
       back.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               finish();
           }
       });
    }


    protected void initData() {
        WebSettings s = mWebView.getSettings();
        s.setBuiltInZoomControls(true);
        s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        s.setUseWideViewPort(true);
        s.setLoadWithOverviewMode(true);
        s.setDomStorageEnabled(true);
        s.setSavePassword(true);
        s.setSaveFormData(true);
        s.setJavaScriptEnabled(true);
        s.setGeolocationEnabled(true);
        s.setDomStorageEnabled(true);
        mWebView.requestFocus();
        mWebView.setWebViewClient(new WebViewClient()
        {
            public boolean shouldOverrideUrlLoading(WebView view,
                                                    String url)
            {
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url)
            {
                super.onPageFinished(view, url);
            }

            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {

            }
        });
        Intent it = getIntent();
        String url = it.getStringExtra("url");
        Uri uri = Uri.parse(url);
        if("http".equals(uri.getScheme()) || "https".equals(uri.getScheme())) {
            mWebView.loadUrl(url);
        }else {
            mWebView.loadData(url,"text/html","utf-8");
            mWebView.getSettings().setTextZoom(300);
            mWebView.getSettings().setSupportZoom(false);
        }
    }
    //防止内存泄漏  activity消失的时候  webview还存在
    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.clearHistory();

            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mWebView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mWebView.onPause();
    }

    protected void initListener() {

    }


}
